
#include<iostream>
#include<map>
#include<vector>
using namespace std;
int findMaxLength(vector<int>& nums) {
    map<int, int> dp;
    dp[0] = -1;
    int flag = 0, count = 0;
    for (int i = 0; i < nums.size(); i++) {
        count += nums[i] == 0 ? -1 : 1;
        if (dp.count(count))
            flag = max(flag, i - dp[count]);
        else
            dp[count] = i;
    }
    return flag;
}
vector<vector<int>> matrixBlockSum(vector<vector<int>>& mat, int k) {
    int sizel = mat.size(), sizer = mat[0].size();
    vector<vector<int>> dp(sizel + 1, vector<int>(sizer + 1)),
        answer(sizel, vector<int>(sizer));
    for (int i = 1; i <= sizel; i++)
        for (int j = 1; j <= sizer; j++)
            dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] +
            mat[i - 1][j - 1];
    for (int i = 0; i < sizel; i++)
        for (int j = 0; j < sizer; j++) {
            int x1, x2, y1, y2;
            x1 = max(0, i - k) + 1;
            y1 = max(0, j - k) + 1;
            x2 = min(sizel - 1, i + k) + 1;
            y2 = min(sizer - 1, j + k) + 1;
            answer[i][j] = dp[x2][y2] + dp[x1 - 1][y1 - 1] -
                dp[x1 - 1][y2] - dp[x2][y1 - 1];
        }
    return answer;
}
int main()
{
    vector<int> v{ 0,0,1,0,1 };
    findMaxLength(v);
    return 0;
}
